//
//  JSViewController05.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/6/19.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class JSViewController05: BaseViewController {
    let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        test03()
        test04()
    }

}

extension JSViewController05 {
    fileprivate func test01() {
        let observable = Observable.of("A", "B", "C")

        observable.subscribe(onNext: { element in
            print(element)
        }, onError: { error in
            print(error)
        }, onCompleted: {
            print("completed")
        }).disposed(by: disposeBag)
    }

    fileprivate func test02() {
        let observable = Observable<Int>.interval(1, scheduler: MainScheduler.instance)

        observable.map {
                "当前索引数：\($0)"
            }
            .bind { [weak self](text) in
                //收到发出的索引数后显示到label上
                print(text)
            }.disposed(by: disposeBag)
    }

    fileprivate func test03() {
        //观察者
        let observer: AnyObserver<String> = AnyObserver { (event) in
            switch event {
            case .next(let data):
                print(data)
            case .error(let error):
                print(error)
            case .completed:
                print("completed")
            }
        }

        let observable = Observable.of("A", "B", "C")
        observable.subscribe(observer).disposed(by: disposeBag)

    }

    fileprivate func test04() {
        //观察者
        let observer: AnyObserver<String> = AnyObserver { [weak self] (event) in
            switch event {
            case .next(let text):
                //收到发出的索引数后显示到label上
                print(text)
            default:
                break
            }
        }

        //Observable序列（每隔1秒钟发出一个索引数）
        let observable = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
        observable
            .map {
                "当前索引数：\($0)"
            }
            .bind(to: observer)
            .disposed(by: disposeBag)
    }


}
